package com.lc.sell.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 图片上传路径和访问路径配置
 */
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowCredentials(true)
                        .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                        .allowedOrigins("*");
            }
        };
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/", "/login","/admin/login","/review/**","/merchant/findAllGoods","/order/**","/doc.html",
                        "/static/**","/js/**","/css/**","/font/**","/image/**","/user/**",
                        "/images/**","/lib/**","/mp3/**","/upload/**","/addresses/**"
                );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * addResourceHandler 访问路径
         * addResourceLocations 添加资源位置（绝对位置）
         */
        // 服务器路径
        //registry.addResourceHandler("/image/**").addResourceLocations("file:/software/upload/");
        // 本地路径
        registry.addResourceHandler("/image/**").addResourceLocations("file:D:\\cx\\lc_sell\\src\\main\\resources\\static\\upload\\");
    }


}
